package com.freesun.shop.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freesun.shop.entity.ProdComm;
import com.freesun.shop.mapper.ProdCommMapper;

import com.freesun.shop.service.ProdCommService;
import com.freesun.shop.vo.ProdCommVo;
import org.apache.dubbo.config.annotation.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * <p>
 * 商品评论 服务实现类
 * </p>
 *
 * @author zf
 * @since 2019-11-13
 */
@Service
public class ProdCommServiceImpl extends ServiceImpl<ProdCommMapper, ProdComm> implements ProdCommService {

    private static DecimalFormat NUM_FORMATE = new DecimalFormat("0.00");
    /**
     * 计算评论的数值
     * @param prodId
     * @return
     */
    @Override
    public ProdCommVo calCommentInfo(Long prodId) {
        ProdCommVo prodCommVo = new ProdCommVo();
        /**
         * 获取好评数量
         */
        Integer praiseNumber = this.baseMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getEvaluate, 0));
        prodCommVo.setPraiseNumber(praiseNumber==null?0:praiseNumber);
        /**
         * 获取总评数量
         */
        Integer totalCommentNumber = this.baseMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId));
        prodCommVo.setNumber(totalCommentNumber==null?0:totalCommentNumber);
        /**
         * 获取好评率
         */
        BigDecimal bigDecimal=null;
        if (prodCommVo.getNumber() == 0) {
            bigDecimal = new BigDecimal(100.00);
        }else{
            double score=((double)prodCommVo.getPraiseNumber()/prodCommVo.getNumber()*100);
            bigDecimal = new BigDecimal(NUM_FORMATE.format(score));
        }
        prodCommVo.setPositiveRating(bigDecimal);
        Integer secondaryNumber = this.baseMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getEvaluate, 1));
        prodCommVo.setSecondaryNumber(secondaryNumber==null?0:secondaryNumber);
        Integer negativeNumber = this.baseMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .eq(ProdComm::getEvaluate, 2));
        prodCommVo.setNegativeNumber(negativeNumber==null?0:negativeNumber);
        Integer picNumber = this.baseMapper.selectCount(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId)
                .isNotNull(ProdComm::getPics));
        prodCommVo.setPicNumber(picNumber==null?0:picNumber);
        return prodCommVo;
    }

    @Override
    public IPage<ProdComm> findByPage(Page<ProdComm> page, ProdComm prodComm) {
        IPage<ProdComm> iPage = this.baseMapper.selectPage(page, new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodComm.getProdId())
                .eq(ProdComm::getStatus,1)
                .eq(prodComm.getEvaluate() != -1, ProdComm::getEvaluate, prodComm.getEvaluate()));
        return iPage;
    }
}
